<?php
/**
 * User: zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/9/29
 * Time: 14:47
 */

namespace App\Providers;


class SendSms
{
    private $appId;

    private $appPassword;

    private $uri;

    public function __construct()
    {
        $this->appId = 'MSG85148937';
        $this->appPassword = 'ZWM2YzEwMTJlOGQ1ZDIyMDBhMTkxMTQ1NDA2YWIxNDk';
        $this->uri = 'https://uc.hznu.edu.cn:8080';
    }

    public function send($title, $content, $role, $number, $realname)
    {
        header('Content-Type: text/html; charset=utf-8');

        $url = $this->uri . '/msg/sendThirdMessage';
        $summary = '';
        switch ($role) {
            case 1 : $summary = '';break;
            case 2 : $summary = '来自人事处的消息';break;
            case 3 : $summary = '来自计财处的消息';break;
            case 4 : $summary = '来自资产管理处的消息';break;
            case 5 : $summary = '来自超级管理员的的消息';break;
        }
        $data = [
            "token" => $this->getToken(),
            "msgtitle" => $title,
            "msgContent" => $content,
            "msgType=" => 1,
            "sendStatus" => 1,
            "sendChannel" => 'SMS',
            "sendMode" => 'normal',
            "receivers" => '[{"type": "User","id": "'.$number.'","name": "'.$realname.'"}]',
            "summary" => $summary,
            "isReply" => 1
        ];

        return Curl::post($url, $data);
    }

    private function getToken()
    {
        header('Content-Type: text/html; charset=utf-8');
        $url = $this->uri . '/msg/getThirdAPIToken';
        $data = Curl::get($url, ['appId' => $this->appId, 'appPassword' => $this->appPassword]);

        return $data['resultData']['token'];
    }
}